package com.hgm.study.viewmodel

import android.util.Log
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Description
import androidx.compose.material.icons.filled.SmartDisplay
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.hgm.study.model.Network
import com.hgm.study.model.entity.Category
import com.hgm.study.model.entity.Swiper
import com.hgm.study.model.entity.Type
import com.hgm.study.model.serice.HomeService

/**
 * 主页面的ViewModel
 */
class MainViewModel : ViewModel() {

      private val homeService = HomeService.instance()

      //分类数据
      var categories by mutableStateOf(
            listOf(
                  Category("思想政治1", "1"),
                  Category("职业道德2", "2"),
                  Category("法律法规3", "3"),
                  Category("诚信自律4", "4")
            )
      )
            private set

      //分类数据是否加载成功
      var categoryLoaded by mutableStateOf(false)
            private set

      //获取网络数据
      suspend fun categoryData() {
            var categoryRes = homeService.getCategory()
            if (categoryRes.code == 0) {
                  //成功
                  categories = categoryRes.data
                  categoryLoaded = true
            } else {
                  //失败
                  val message = categoryRes.message
                  Log.e("hgm", "categoryData网络加载信息: $message")
            }
      }

      //当前分类下标
      var categoryIndex by mutableStateOf(0)
            private set

      //通过调用这个方法更新下标，保证不被外部修改
      fun updateCategoryIndex(index: Int) {
            categoryIndex = index
      }


      //2.类型数据
      val types by mutableStateOf(
            listOf(
                  Type("相关知识", Icons.Default.Description),
                  Type("视频课程", Icons.Default.SmartDisplay)
            )
      )

      //默认显示文章列表
      var showArticleList by mutableStateOf(true)
            private set

      //当前类型选中下标
      var currentTypeIndex by mutableStateOf(0)
            private set

      //更新分类下标
      fun updateTypeIndex(index: Int) {
            currentTypeIndex = index
            showArticleList = currentTypeIndex == 0
      }


      //3.轮播图数据
      var swiperLoader by mutableStateOf(false)
            private set

      var swipers by mutableStateOf(
            listOf(
                  Swiper("")
            )
      )
            private set

      suspend fun swiperData() {
            var swiperRes = homeService.getBanner()
            if (swiperRes.code == 0) {
                  swipers = swiperRes.data
                  swiperLoader = true
            } else {
                  val message = swiperRes.message
                  Log.e("hgm", "swiperData网络加载信息: $message")
            }
      }


      //4.通知栏数据
      val notifications = listOf(
            "中华人民共和国人社部疫情防控公告第三十二期（2）",
            "广东省新冠肺炎患者从去年春季开始日渐增多累计数据达到高峰",
            "广东省广州市海珠区防控疫情情况公告第12期",
            "广东新冠肺炎单日突破新高"
      )


}